home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / vsa25611.zip / VSA_DEMO.C < prev    next >
C/C++ Source or Header  |  1992-07-04  |  11KB  |  362 lines

  1. /*..........................................................................*/
  2. /*                              VSA_DEMO.C              7-4-92              */
  3. /*                                                                          */
  4. /*  This is the "C" source code for the VSA_DEMO.EXE program.  This program */
  5. /*  demonstrates the usage of the VSA256 Graphics Library, Version 1.1      */
  6. /*  functions.                                                              */
  7. /*                                                                          */
  8. /*            Copyright Spyro Gumas, 1992.  All Rights Reserved.            */
  9. /*..........................................................................*/
  10.  
  11. #include<stdlib.h>
  12. #include<math.h>
  13. #include<limits.h>
  14. #include<bios.h>
  15.  
  16. #include "vsa.h"             /* Required to support VSA256 Graphics Library */
  17.  
  18.  
  19. void cube(int,int,int);
  20. void rainbow_lut();
  21. void special_lut();
  22. void color_bar(int,int);
  23. void banner(int,int);
  24. void image(int,int);
  25.  
  26. float SIN_LUT[1024];
  27.  
  28. void main()
  29. {
  30.     int i,x,y,size;
  31.     unsigned vmode,xx,yy,a,b,c,d;
  32.     unsigned char color,j,jj,row,col;
  33.     char text[100],your_name[80];
  34.     unsigned char color_array[768],save_color_array[768];
  35.     unsigned char red[256],green[256],blue[256];
  36.     srand(1);
  37. /*..........................................................................*/
  38. /*                      Initialize sin look up table.                       */
  39. /*  Index 'i' goes from 0 to 1023 and is equivalent to 0 to 360 degrees.    */
  40. /*..........................................................................*/
  41.     for(i=0;i<1024;i++)
  42.         SIN_LUT[i] = sin(i*6.28/1024.0);
  43. /*..........................................................................*/
  44. /*               Initialize video mode and VSA256 environment.              */
  45. /*               Valid modes are: 100h, 101h, 103h, and 105h.               */
  46. /*..........................................................................*/
  47.     printf("\n");
  48.     printf("Input VESA standard Video Mode (hex)\n");
  49.     printf("(100, 101, 103, or 105): ");
  50.     scanf("%x",&vmode);
  51.     vsa_init(vmode);
  52.     xx = XResolution;
  53.     yy = YResolution;
  54.     vsa_set_color(1);
  55.     vsa_move_to(0,0);
  56.     vsa_rect(xx-1,yy-1);
  57.  
  58. /*..........................................................................*/
  59. /*             Draw color look up table at bottom of screen.                */
  60. /*..........................................................................*/
  61.     rainbow_lut();
  62.     a = .125*xx;
  63.     b = .81*yy;
  64.     color_bar(a,b);
  65. /*..........................................................................*/
  66. /*            Draw "random" cubes enclosed by rectangle                     */
  67. /*..........................................................................*/
  68.     a = .6*xx;
  69.     b = .4*yy;
  70.     c = .88*xx;
  71.     d = .72*yy;
  72.     vsa_move_to(a,b);
  73.     vsa_set_color(180);
  74.     vsa_rect(c,d);
  75.     vsa_move_to(a+1,b+1);
  76.     vsa_set_color(20);
  77.     vsa_rect_fill(c-1,d-1);
  78.     for(i=0;i<16;i++)
  79.         {
  80.             x = a+5+.23*xx*(float)rand()/RAND_MAX;
  81.             y = b+5+.23*yy*(float)rand()/RAND_MAX;
  82.             size = .05*xx*(float)rand()/RAND_MAX;
  83.             color = (unsigned char) i;
  84.             vsa_set_color(color);
  85.             cube(x,y,size);
  86.         }
  87.     row = (.74*yy)/YCharSize;
  88.     col = (.64*xx)/XCharSize;
  89.     row = (d)/YCharSize + 1;
  90.     col = (a+(c-a)/2)/XCharSize - 12;
  91.     sprintf(text,"Lines using `vsa_line_to'");
  92.     vsa_write_string(row,col,250,text);
  93. /*..........................................................................*/
  94. /*                          Draw a banner                                   */
  95. /*..........................................................................*/
  96.     a = .55*xx;
  97.     b = .06*yy;
  98.     banner(a,b);
  99. /*..........................................................................*/
  100. /*                    Draw 2D sine-cosine image                             */
  101. /*..........................................................................*/
  102.     a = .08*xx;
  103.     b = .26*yy;
  104.     image(a,b);
  105. /*..........................................................................*/
  106. /*                        Using Text Cursor Mode 1.                         */
  107. /*..........................................................................*/
  108.     row = .1*yy/YCharSize;
  109.     col = .05*xx/XCharSize;
  110.     vsa_set_text_cursor_mode(1);
  111.     vsa_write_string(row,col,250,"Please Enter Your Name: ");
  112.     scanf("%s",&your_name);
  113.  
  114.     vsa_set_text_color(200);
  115.     vsa_set_text_cursor(row+1,col);
  116.     vsa_write_string_alt("Hello ");
  117.     vsa_write_string_alt(your_name);
  118.     vsa_write_string_alt(", Hit any key to bail.");
  119. /*..........................................................................*/
  120. /*    Now do sliding blue color effect, until someone presses a key.        */
  121. /*..........................................................................*/
  122.     vsa_read_color_block(251,5,save_color_array);
  123.     for(j=0;j<5;j++)
  124.         {
  125.             red[j+251]   = save_color_array[3*j];
  126.             green[j+251] = save_color_array[3*j+1];
  127.             blue[j+251]  = save_color_array[3*j+2];
  128.         }
  129. INFINITE_LOOP:
  130.     for(i=32;i<256;i++)
  131.         {
  132.             j = (unsigned char) i;
  133.             vsa_read_color_register(j,&red[j],&green[j],&blue[j]);
  134.             vsa_write_color_register(j,0,0,63);
  135.             if(j <= 36)
  136.                 jj = (unsigned char)(j-37);
  137.             else
  138.                 jj = (unsigned char)(j-5);
  139.             vsa_write_color_register(jj,red[jj],green[jj],blue[jj]);
  140. /*.....
  141.             For Borland C, Use this line instead.
  142.             if(bioskey(1))
  143. .....*/
  144.             if(_bios_keybrd(_KEYBRD_READY))
  145.                 goto BAIL;
  146.         }
  147.     goto INFINITE_LOOP;
  148. /*..........................................................................*/
  149. /*           Restore text video mode and print information.                 */
  150. /*..........................................................................*/
  151. BAIL:
  152.     vsa_set_svga_mode(0x3);
  153.     vsa_about();
  154.     return;
  155. }
  156.  
  157. void cube(int x,int y,int size)
  158. {
  159.     int sizeb;
  160.     sizeb = size/2;
  161.     vsa_move_to(x,y);
  162.     vsa_line_to(x+size,y);
  163.     vsa_line_to(x+size,y+size);
  164.     vsa_line_to(x,y+size);
  165.     vsa_line_to(x,y);
  166.     vsa_line_to(x+sizeb,y+sizeb);
  167.     vsa_line_to(x+sizeb+size,y+sizeb);
  168.     vsa_line_to(x+sizeb+size,y+sizeb+size);
  169.     vsa_line_to(x+sizeb,y+sizeb+size);
  170.     vsa_line_to(x+sizeb,y+sizeb);
  171.     vsa_move_to(x+size,y);
  172.     vsa_line_to(x+sizeb+size,y+sizeb);
  173.     vsa_move_to(x+size,y+size);
  174.     vsa_line_to(x+sizeb+size,y+sizeb+size);
  175.     vsa_move_to(x,y+size);
  176.     vsa_line_to(x+sizeb,y+sizeb+size);
  177.     return;
  178. }
  179.  
  180. void rainbow_lut()
  181. {
  182.     int i,start,count;
  183.     unsigned char color_array[768];
  184.     for(i=0;i<224;i++)
  185.         {
  186.             color_array[3*i+2]=0;
  187.             color_array[3*i+1]=0;
  188.             color_array[3*i]=0;
  189.         }
  190. /*................................ RED .....................................*/
  191.     for(i=0;i<56;i++)
  192.         {
  193.                 color_array[3*i] = 63*sin((i*6.28)/112.0);
  194.         }
  195. /*............................... BLUE .....................................*/
  196.     for(i=0;i<126;i++)
  197.         {
  198.                 color_array[3*i+2] = 63*sin((i*6.28)/252.0);
  199.         }
  200. /*............................... GREEN ....................................*/
  201.     for(i=96;i<210;i++)
  202.         {
  203.                 color_array[3*i+1] = 63*sin(((i-90)*6.28)/252.0);
  204.         }
  205. /*................................ RED .....................................*/
  206.     for(i=140;i<224;i++)
  207.         {
  208.                 color_array[3*i]   = 63*sin(((i-140)*6.28)/280.0);
  209.         }
  210.     start = 32;
  211.     count = 224;
  212.     vsa_write_color_block(start,count,color_array);
  213.     return;
  214. }
  215.  
  216. void special_lut()
  217. {
  218.     int i,start,count;
  219.     unsigned char color_array[768];
  220.     for(i=0;i<256;i++)
  221.         {
  222.             color_array[3*i+2]=0;
  223.             color_array[3*i+1]=0;
  224.             color_array[3*i]=0;
  225.         }
  226. /*............................... BLUE .....................................*/
  227.     for(i=0;i<128;i++)
  228.         {
  229.                 color_array[3*i+2] = 63*sin((i*6.28)/256.0);
  230.         }
  231. /*............................... GREEN ....................................*/
  232.     for(i=48;i<240;i++)
  233.         {
  234.                 color_array[3*i+1] = 63*sin(((i-48)*6.28)/384.0);
  235.         }
  236. /*................................ RED .....................................*/
  237.     for(i=128;i<256;i++)
  238.         {
  239.                 color_array[3*i]   = 63*sin(((i-128)*6.28)/512.0);
  240.         }
  241.     start = 0;
  242.     count = 256;
  243.     vsa_write_color_block(start,count,color_array);
  244.     return;
  245. }
  246.  
  247. void color_bar(x0,y0)
  248. int x0,y0;
  249. {
  250.     char text[100];
  251.     unsigned char row,col;
  252.     int i;
  253.     unsigned xx,yy,a,b;
  254.     float c;
  255.     xx = XResolution;
  256.     yy = YResolution;
  257. /*..........................................................................*/
  258. /*     Draw outline for color bar.                                          */
  259. /*..........................................................................*/
  260.     vsa_set_color(15);
  261.     vsa_move_to(x0-1,y0-1);
  262.     a = .75*xx;
  263.     b = .065*yy;
  264.     vsa_rect(x0+a+1,y0+b+1);
  265.     c = (float)a/256;
  266.     for(i=0;i<256;i++)
  267.         {
  268.             vsa_set_color((unsigned char)i);
  269.             vsa_move_to(x0+(unsigned)(i*c),y0);
  270.             vsa_rect_fill(x0+(unsigned)(c+i*c),y0+b);
  271.         }
  272.     row = (y0+b+1)/YCharSize + 1;
  273.     col = (x0+a/2)/XCharSize - 31;
  274.     sprintf(text,"Color Look Up Table Manipulation using");
  275.     vsa_write_string(row,col,63,text);
  276.     sprintf(text," `vsa_write_color_block'");
  277.     vsa_write_string(row,col+38,63,text);
  278.     sprintf(text,"'vsa_read_color_register' and");
  279.     vsa_write_string(row+1,col,63,text);
  280.     sprintf(text,"`vsa_write_color_register'.");
  281.     vsa_write_string(row+1,col+31,63,text);
  282.     return;
  283. }
  284.  
  285. void banner(int x,int y)
  286. {
  287.     int x0,y0,x1,y1;
  288.     unsigned char row,col;
  289.     char text[100];
  290.     unsigned xx,yy,a,b;
  291.     xx = XResolution;
  292.     yy = YResolution;
  293.     a = .40*xx;
  294.     b = .17*yy;
  295.     vsa_move_to(x,y);
  296.     vsa_set_color(1);
  297.     vsa_rect_fill(x+a,y+b);
  298.     vsa_move_to(x+5,y+5);
  299.     vsa_set_color(0);
  300.     vsa_rect_fill(x+a-5,y+b-5);
  301.     row = (y+6+b/2)/YCharSize - 1;
  302.     col = (x+(a+5)/2)/XCharSize - 13;
  303.     sprintf(text,"VSA256 GRAPHICS LIBRARY");
  304.     vsa_write_string(row,col,200,text);
  305.     sprintf(text,"for C Programmers");
  306.     vsa_write_string(row+1,col,200,text);
  307.     sprintf(text,"Copyright Spyro Gumas 1992");
  308.     vsa_write_string(row+2,col,200,text);
  309.     vsa_set_text_color(250);
  310.     vsa_write_char(row+1,col+18,'V');
  311.     vsa_write_char(row+1,col+19,'1');
  312.     vsa_write_char(row+1,col+20,'.');
  313.     vsa_write_char(row+1,col+21,'1');
  314.     row = (y+b)/YCharSize + 1;
  315.     col = (x+(a+5)/2)/XCharSize - 13;
  316.     sprintf(text,"Character and Text Strings");
  317.     vsa_write_string(row,col,2,text);
  318.     sprintf(text,"using `vsa_write_char'");
  319.     vsa_write_string(row+1,col,2,text);
  320.     sprintf(text,"and `vsa_write_string'");
  321.     vsa_write_string(row+2,col,2,text);
  322.     return;
  323. }
  324.  
  325. void image(int x,int y)
  326. {
  327.     int i,j;
  328.     long ii,jj,z1,z2;
  329.     unsigned char array[1024],row,col;
  330.     char text[100];
  331.     unsigned xx,yy,a,b;
  332.     xx = XResolution;
  333.     yy = YResolution;
  334.     a = .4*xx;
  335.     b = .26*yy;
  336.     z1 = 2*1024L/a;
  337.     z2 = 1024L/b;
  338.     vsa_move_to(x-2,y-2);
  339.     vsa_set_color(250);
  340.     vsa_rect(x+a+1,y+b+1);
  341.     for(j=0;j<b;j++)
  342.         {
  343.             for(i=0;i<a;i++)
  344.                 {
  345.                     ii = (i*z1) & 0x000003ff;
  346.                     jj = (j*z2+256) & 0x000003ff;
  347. /*.....
  348.                     array[i] = 144+112*sin(i*6.28/c)*cos(j*6.28/c);
  349. .....*/
  350.                     array[i] = 144+112*SIN_LUT[ii]*SIN_LUT[jj];
  351.                 }
  352.             vsa_raster_line(x,x+a-1,y+j,array);
  353.         }
  354.     row = (y+b+1)/YCharSize + 1;
  355.     col = (x+a/2)/XCharSize - 17;
  356.     sprintf(text,"2D Images Using `vsa_raster_line()'");
  357.     vsa_write_string(row,col,100,text);
  358.     return;
  359. }
  360.  
  361.  
  362.